import {
  Controller,
  Get,
  Post,
  Body,
  Param,
  UseGuards,
  UploadedFile,
  UseInterceptors,
} from '@nestjs/common';
import { FileInterceptor } from '@nestjs/platform-express';
import { CatsService } from './cats.service';
import { CreateCatDto } from './dto/create-cat.dto';
import { Cat } from './entities/cat.entity';
import {
  ApiOperation,
  ApiResponse,
  ApiTags,
  ApiBody,
  ApiConsumes,
} from '@nestjs/swagger';
import { Roles } from '../auth/decorators/roles.decorator';
import { RolesGuard } from '../auth/guards/roles.guard';
import { JwtAuthGuard } from '../auth/guards/jwt-auth.guard';

@ApiTags('cats')
@Controller('cats')
@UseGuards(JwtAuthGuard)
export class CatsController {
  constructor(private readonly catsService: CatsService) {}

  @Post()
  @Roles('admin')
  @UseGuards(RolesGuard)
  @ApiOperation({ summary: '添加一只新猫咪' })
  @ApiResponse({ status: 201, description: '猫咪已成功添加', type: Cat })
  create(@Body() createCatDto: CreateCatDto): Cat {
    return this.catsService.create(createCatDto);
  }

  @Post('with-avatar')
  @Roles('admin')
  @UseGuards(RolesGuard)
  @UseInterceptors(FileInterceptor('avatar'))
  @ApiOperation({ summary: '添加一只带头像的新猫咪' })
  @ApiConsumes('multipart/form-data')
  @ApiBody({
    description: '创建猫咪带头像',
    schema: {
      type: 'object',
      properties: {
        name: { type: 'string' },
        age: { type: 'number' },
        breed: { type: 'string', nullable: true },
        avatar: {
          type: 'string',
          format: 'binary',
        },
      },
    },
  })
  @ApiResponse({ status: 201, description: '猫咪已成功添加', type: Cat })
  async createWithAvatar(
    @Body() createCatDto: CreateCatDto,
    @UploadedFile() avatar: Express.Multer.File,
  ): Promise<Cat> {
    return this.catsService.createWithAvatar(createCatDto, avatar);
  }

  @Get()
  @ApiOperation({ summary: '获取所有猫咪' })
  @ApiResponse({
    status: 200,
    description: '返回所有猫咪列表',
    type: Cat,
    isArray: true,
  })
  findAll(): Cat[] {
    return this.catsService.findAll();
  }

  @Get(':id')
  @ApiOperation({ summary: '根据ID获取猫咪' })
  @ApiResponse({ status: 200, description: '返回指定ID的猫咪', type: Cat })
  @ApiResponse({ status: 404, description: '猫咪未找到' })
  findOne(@Param('id') id: string): Cat {
    return this.catsService.findOne(+id);
  }
}
